<?php
/**
 * Project: Recipe
 * File: SpecialAction.class.php
 * Author: Xnfy520@gmail.com
 * Date: 12-5-26
 * Time: 上午11:05
 */

class SpecialAction extends CommonAction{

	public function index(){
		$specialrecipe = D('SpecialArticle');
		import("ORG.Util.emit_page");
		$count = $specialrecipe->where('publish=1')->count();
		$page = new page($count,12);
		$specialarticleinfo = $specialrecipe->relation(true)->where('publish')->limit($page->setlimit())->select();
		$this->assign('specialarticleinfo',$specialarticleinfo);
		$this->assign('fpage',$page->fpage(array(5,6,7)));

		//最新发布
		$newrecipe = $specialrecipe->relation(true)->where('publish=1')->order('created desc')->limit(5)->select();
		$this->assign('newrecipe',$newrecipe);

		$this->display('menu');
	}


	function del(){
		if(isset($_GET['cid']) && !empty($_GET['cid'])){
			if(isset($_GET['id']) && !empty($_GET['id'])){
				$srcdir = './Public/Content/SpecialArticle/';

				$recipe = M('SpecialArticle');

				$recipenutritions = M('RecipeNutritions');
				$recipemain = M('RecipeMain');
				$recipeaccessory = M('RecipeAccessory');
				$recipecondiment = M('RecipeCondiment');

				$specialcomments = M('SpecialComments');

				$map['recipecategoryid'] = $_GET['cid'];
				$map['id'] = $_GET['id'];
				$recipeinfo = $recipe->where($map)->find();

				if($recipe->where($map)->delete()){

					if(!empty($recipeinfo['image'])){
						unlink($srcdir.'Big_'.$recipeinfo['image']);
						unlink($srcdir.'Small_'.$recipeinfo['image']);
					}

					$deletes['recipetypeid'] = $recipeinfo['recipetypeid'];
					$deletes['recipecategoryid'] = $_GET['cid'];
					$deletes['recipearticleid'] = $_GET['id'];
					$recipemain->where($deletes)->delete();
					$recipeaccessory->where($deletes)->delete();
					$recipecondiment->where($deletes)->delete();

					$del['sectionid'] = $recipeinfo['recipetypeid'];
					$del['categoryid'] = $_GET['cid'];
					$del['articleid'] = $_GET['id'];
					$recipenutritions->where($del)->delete();

					$specialcomments->where('categoryid='.$_GET['cid'].' AND articleid='.$_GET['id'])->delete();

					$this->success('删除成功');

				}else{

					$this->error('删除失败');

				}

			}else{
				$this->error('异常操作');
			}
		}else{
			$this->error('异常操作');
		}
	}

	function favourite(){
		if(isset($_GET['sid']) && isset($_GET['cid']) && isset($_GET['id'])){
			if(!empty($_GET['sid']) && !empty($_GET['cid']) && !empty($_GET['id'])){
				if(isset($_SESSION['username']) && isset($_SESSION['userid'])){
					$favourite = M('RecipeFavourite');
					$map['founder'] = $_SESSION['userid'];
					$map['sectionid'] = $_GET['sid'];
					$map['categoryid'] = $_GET['cid'];
					$map['articleid'] = $_GET['id'];
					$favouriteinfo = $favourite->where($map)->count();
					if($favouriteinfo>0){
						$this->error('该菜谱已收藏');
					}else{
						$map['created'] = time();
						if($favourite->add($map)){
							$this->success('添加收藏成功');
						}else{
							$this->error('添加收藏失败');
						}
					}

				}
			}else{
				$this->error('异常操作');
			}
		}else{
			$this->error('异常操作');
		}
	}

	function delfavourite(){
		if(isset($_GET['sid']) && isset($_GET['cid']) && isset($_GET['id'])){
			if(!empty($_GET['sid']) && !empty($_GET['cid']) && !empty($_GET['id'])){
				if(isset($_SESSION['username']) && isset($_SESSION['userid'])){
					$favourite = M('RecipeFavourite');
					$map['founder'] = $_SESSION['userid'];
					$map['sectionid'] = $_GET['sid'];
					$map['categoryid'] = $_GET['cid'];
					$map['articleid'] = $_GET['id'];
					$favouriteinfo = $favourite->where($map)->count();
					if($favouriteinfo>0){
						if($favourite->where($map)->delete()){
							$this->success('删除收藏成功');
						}else{
							$this->error('删除收藏失败');
						}
					}
				}
			}else{
				$this->error('异常操作');
			}
		}else{
			$this->error('异常操作');
		}
	}

	function content(){

		if(isset($_GET['cid']) && isset($_GET['id'])){
			if(!empty($_GET['cid']) && !empty($_GET['id'])){
				$recipe = D('SpecialArticle');
				$foodarticle = M('FoodArticle');
				$setdeal = M('SetDeal');
				$fooddeal = M('FoodDeal');
				$recipenutritions = M('RecipeNutritions');
				import("ORG.Util.emit_page");

				$recipe->where('recipecategoryid='.$_GET['cid'].' AND '.'id='.$_GET['id'])->setInc('downloads');
				$map['recipecategoryid'] = $_GET['cid'];
				$recipeinfo = $recipe->relation(true)->where($map)->find($_GET['id']);
				$this->assign('recipeinfo',$recipeinfo);

				if(empty($recipeinfo)){
					$this->redirect('Special/index');
				}

				$return['sectionid'] = $recipeinfo['recipetypeid'];
				$return['categoryid'] = $_GET['cid'];
				$return['articleid'] = $_GET['id'];

				$recipenutritionsinfo = $recipenutritions->where($return)->find();
				$this->assign('recipenutritionsinfo',$recipenutritionsinfo);

				$mainsinfo = array();
				$accessoryinfo = array();
				$condimentinfo = array();

				if(!empty($recipeinfo['recipemain'])){

					for($i=0; $i<count($recipeinfo['recipemain']); $i++){
						$mainsinfo[$i]['fooddeal'] = $fooddeal->where('articleid='.$recipeinfo['recipemain'][$i]['foodarticleid'].' AND '.'unit='.$recipeinfo['recipemain'][$i]['deal'])->getField('deal');
						$mainsinfo[$i]['foodname'] = $recipeinfo['recipemain'][$i]['foodname'];
						$mainsinfo[$i]['foodimage'] = $foodarticle->where('id='.$recipeinfo['recipemain'][$i]['foodarticleid'])->getField('image');
						$mainsinfo[$i]['foodamount'] = $recipeinfo['recipemain'][$i]['amount'];
						$mainsinfo[$i]['foodunit'] = $setdeal->where('id='.$recipeinfo['recipemain'][$i]['deal'])->getField('name');
						$mainsinfo[$i]['foodsectionid'] = $recipeinfo['recipemain'][$i]['foodsectionid'];
						$mainsinfo[$i]['foodcategoryid'] = $recipeinfo['recipemain'][$i]['foodcategoryid'];
						$mainsinfo[$i]['foodid'] = $recipeinfo['recipemain'][$i]['foodarticleid'];
						$mainsinfo[$i]['foodtotal'] = $mainsinfo[$i]['fooddeal']*$mainsinfo[$i]['foodamount'];
					}
				}
				$this->assign('mainsinfo',$mainsinfo);

				if(!empty($recipeinfo['recipeaccessory'])){

					for($ia=0; $ia<count($recipeinfo['recipeaccessory']); $ia++){
						$accessoryinfo[$ia]['fooddeal'] = $fooddeal->where('articleid='.$recipeinfo['recipeaccessory'][$ia]['foodarticleid'].' AND '.'unit='.$recipeinfo['recipeaccessory'][$ia]['deal'])->getField('deal');
						$accessoryinfo[$ia]['foodname'] = $recipeinfo['recipeaccessory'][$ia]['foodname'];
						$accessoryinfo[$ia]['foodimage'] = $foodarticle->where('id='.$recipeinfo['recipeaccessory'][$ia]['foodarticleid'])->getField('image');
						$accessoryinfo[$ia]['foodamount'] = $recipeinfo['recipeaccessory'][$ia]['amount'];
						$accessoryinfo[$ia]['foodunit'] = $setdeal->where('id='.$recipeinfo['recipeaccessory'][$ia]['deal'])->getField('name');
						$accessoryinfo[$ia]['foodsectionid'] = $recipeinfo['recipeaccessory'][$ia]['foodsectionid'];
						$accessoryinfo[$ia]['foodcategoryid'] = $recipeinfo['recipeaccessory'][$ia]['foodcategoryid'];
						$accessoryinfo[$ia]['foodid'] = $recipeinfo['recipeaccessory'][$ia]['foodarticleid'];
						$accessoryinfo[$ia]['foodtotal'] = $accessoryinfo[$ia]['fooddeal']*$accessoryinfo[$ia]['foodamount'];
					}
				}
				$this->assign('accessoryinfo',$accessoryinfo);

				if(!empty($recipeinfo['recipecondiment'])){

					for($i=0; $i<count($recipeinfo['recipecondiment']); $i++){
						$condimentinfo[$i]['fooddeal'] = $fooddeal->where('articleid='.$recipeinfo['recipecondiment'][$i]['foodarticleid'].' AND '.'unit='.$recipeinfo['recipecondiment'][$i]['deal'])->getField('deal');
						$condimentinfo[$i]['foodname'] = $recipeinfo['recipecondiment'][$i]['foodname'];
						$condimentinfo[$i]['foodimage'] = $foodarticle->where('id='.$recipeinfo['recipecondiment'][$i]['foodarticleid'])->getField('image');
						$condimentinfo[$i]['foodamount'] = $recipeinfo['recipecondiment'][$i]['amount'];
						$condimentinfo[$i]['foodunit'] = $setdeal->where('id='.$recipeinfo['recipecondiment'][$i]['deal'])->getField('name');
						$condimentinfo[$i]['foodsectionid'] = $recipeinfo['recipecondiment'][$i]['foodsectionid'];
						$condimentinfo[$i]['foodcategoryid'] = $recipeinfo['recipecondiment'][$i]['foodcategoryid'];
						$condimentinfo[$i]['foodid'] = $recipeinfo['recipecondiment'][$i]['foodarticleid'];
						$condimentinfo[$i]['foodtotal'] = $condimentinfo[$i]['fooddeal']*$condimentinfo[$i]['foodamount'];
					}
				}
				$this->assign('condimentinfo',$condimentinfo);


				$user = D('User');
				$usermap['id'] = $_SESSION['userid'];
				$userinfo = $user->where($usermap)->find();
				$this->assign('userinfo',$userinfo);

				$comments = M('SpecialComments');

				$commentsmap['categoryid'] = $_GET['cid'];
				$commentsmap['articleid'] = $_GET['id'];

				$count = $comments->where($commentsmap)->count();

				$page = new page($count,3);
				$commentsinfo = $comments->limit($page->setlimit())->where($commentsmap)->order('id desc')->select();
				$avg = $comments->where($commentsmap)->avg('onlevel');
				$this->assign('levelavg',(int)$avg);
				$this->assign('commentsinfo',$commentsinfo);
				$this->assign('fpage',$page->fpage(array(5,6,7)));

				$this->display('ingredient');
			}else{
				$this->error('异常操作');
			}
		}else{
			$this->error('异常操作');
		}


	}

	function add(){

		$foodsection = M('FoodSection');
		$foodsectioninfo = $foodsection->field('id,name')->order('id desc')->select();
		$this->assign('foodsectioninfo', $foodsectioninfo);

		$articles = array(
			'Recipe',
			'Menu',
			'Sauce',
			'Tips & Garnishment',
		);

		$this->assign('articles', $articles);

		$setcookmethod = M('SetCookMethod');
		$setcookmethodinfo = $setcookmethod->order('id desc')->select();
		$this->assign('setcookmethodinfo', $setcookmethodinfo);

		$setcourse = M('SetCourse');
		$setcourseinfo = $setcourse->order('id desc')->select();
		$this->assign('setcourseinfo', $setcourseinfo);

		$setdietaryneeds = M('SetDietaryNeeds');
		$setdietaryneedsdinfo = $setdietaryneeds->order('id desc')->select();
		$this->assign('setdietaryneedsinfo', $setdietaryneedsdinfo);

		$setservepeople = M('SetServePeople');
		$setservepeopleinfo = $setservepeople->order('id desc')->select();
		$this->assign('setservepeopleinfo', $setservepeopleinfo);

		$seteasyhard = M('SetEasyHard');
		$seteasyhardinfo = $seteasyhard->order('id desc')->select();
		$this->assign('seteasyhardinfo', $seteasyhardinfo);

		$settotaltime = M('SetTotalTime');
		$settotaltimeinfo = $settotaltime->order('id desc')->select();
		$this->assign('settotaltimeinfo', $settotaltimeinfo);

		$setoccasion = M('SetOccasion');
		$setoccasioninfo = $setoccasion->order('id desc')->select();
		$this->assign('setoccasioninfo', $setoccasioninfo);

		$setdeal = M('SetDeal');
		$setdealinfo = $setdeal->order('id desc')->select();
		$this->assign('setdealinfo', $setdealinfo);

		$recipecategory = M('RecipeCategory');
		$recipecategoryinfo = $recipecategory->order('name')->select();
		$this->assign('recipecategoryinfo', $recipecategoryinfo);
		$this->display();
	}

	function edit(){

		if(!empty($_GET['sid']) && !empty($_GET['cid']) && !empty($_GET['id'])){
			if(isset($_GET['sid']) && isset($_GET['cid']) && isset($_GET['id'])){

				$foodsection = M('FoodSection');
				$foodsectioninfo = $foodsection->field('id,name')->order('id desc')->select();
				$this->assign('foodsectioninfo', $foodsectioninfo);

				$articles = array(
					'Recipe',
					'Menu',
					'Sauce',
					'Tips & Garnishment',
				);

				$this->assign('articles', $articles);

				$setcookmethod = M('SetCookMethod');
				$setcookmethodinfo = $setcookmethod->order('id desc')->select();
				$this->assign('setcookmethodinfo', $setcookmethodinfo);

				$setcourse = M('SetCourse');
				$setcourseinfo = $setcourse->order('id desc')->select();
				$this->assign('setcourseinfo', $setcourseinfo);

				$setdietaryneeds = M('SetDietaryNeeds');
				$setdietaryneedsdinfo = $setdietaryneeds->order('id desc')->select();
				$this->assign('setdietaryneedsinfo', $setdietaryneedsdinfo);

				$setservepeople = M('SetServePeople');
				$setservepeopleinfo = $setservepeople->order('id desc')->select();
				$this->assign('setservepeopleinfo', $setservepeopleinfo);

				$seteasyhard = M('SetEasyHard');
				$seteasyhardinfo = $seteasyhard->order('id desc')->select();
				$this->assign('seteasyhardinfo', $seteasyhardinfo);

				$settotaltime = M('SetTotalTime');
				$settotaltimeinfo = $settotaltime->order('id desc')->select();
				$this->assign('settotaltimeinfo', $settotaltimeinfo);

				$setoccasion = M('SetOccasion');
				$setoccasioninfo = $setoccasion->order('id desc')->select();
				$this->assign('setoccasioninfo', $setoccasioninfo);

//				$setdeal = D('SetDeal');
//				$setdealinfo = $setdeal->relation(true)->order('id desc')->where('publish=1')->select();
//				$this->assign('setdealinfo', $setdealinfo);

				$specialcategory = M('SpecialCategory');
				$specialcategoryinfo = $specialcategory->order('name')->select();
				$this->assign('specialcategoryinfo', $specialcategoryinfo);


				$specialarticle = D('SpecialArticle');
				$map['recipetypeid'] = $_GET['sid'];
				$map['recipecategoryid'] = $_GET['cid'];
				$map['id'] = $_GET['id'];
				$specialarticleinfo = $specialarticle->relation(true)->where($map)->find();
				$this->assign('recipearticleinfo', $specialarticleinfo);

				$this->display();

			}else{
				$this->error('异常操作');
			}
		}else{
			$this->error('异常操作');
		}
	}


	function insert(){
		if(!empty($_POST['recipetype']) && isset($_POST['recipetype'])){
			if($_POST['recipetype']=='Special' && $_POST['recipetypeid']==2){

				$tmpdir = './Public/Content/tmp/';
				$srcdir = './Public/Content/SpecialArticle/';
				$recipe = D('SpecialArticle');

				$recipemain = M('RecipeMain');
				$recipeaccessory = M('RecipeAccessory');
				$recipecondiment = M('RecipeCondiment');
				$recipenutritions = M('RecipeNutritions');

				$user = M('User');
				$userinfo = $user->find($_SESSION['userid']);
				$orgdatas = $_POST;

				if($recipeinfo = $recipe->create()){

					$recipeinfo['founder'] = $_SESSION['userid'];

					if($recipeid = $recipe->add($recipeinfo)){

						$returndatas = $this->calculate($orgdatas);
						$returndatas['sectionid'] = 2;
						$returndatas['categoryid'] = $_POST['recipecategoryid'];
						$returndatas['articleid'] = $recipeid;
						$recipenutritions->add($returndatas);

						if(!empty($recipeinfo['image'])){
							copy($tmpdir.'Big_'.$recipeinfo['image'], $srcdir.'Big_'.$recipeinfo['image']);
							copy($tmpdir.'Small_'.$recipeinfo['image'], $srcdir.'Small_'.$recipeinfo['image']);
							if($dirsrc = opendir($tmpdir)){
								while($files = readdir($dirsrc)){
									if($files!='.' && $files!='..'){
										$pathfile = $tmpdir.$files;
										if(is_file($pathfile)){
											unlink($pathfile);
										}
									}
								}
							}
							$user->where('id='.$userinfo['id'])->setInc('exp',25);
						}else{
							$user->where('id='.$userinfo['id'])->setInc('exp',10);
						}

						for($i=0; $i<count($_POST['main']); $i++){
							if(empty($_POST['main'][$i])){
								continue;
							}else{
								$mainmap['amount'] = $_POST['mainamount'][$i];
								$mainmap['deal'] = $_POST['maindeal'][$i];
								$mainmap['preparation'] = $_POST['mainpreparation'][$i];
								$mainmap['foodsectionid'] = $_POST['mainfoodsectionid'][$i];
								$mainmap['foodcategoryid'] = $_POST['mainfoodcategoryid'][$i];
								$mainmap['foodarticleid'] = $_POST['mainfoodarticleid'][$i];
								$mainmap['foodname'] = $_POST['main'][$i];
								$mainmap['recipecategoryid'] = $_POST['recipecategoryid'];
								$mainmap['recipetype'] = $_POST['recipetype'];
								$mainmap['recipetypeid'] = $_POST['recipetypeid'];
								$mainmap['recipearticleid'] = $recipeid;
								//		dump($mainmap);
								$recipemain->add($mainmap);
							}
						}

						for($i=0; $i<count($_POST['other']); $i++){
							if(empty($_POST['other'][$i])){
								continue;
							}else{
								$accessorymap['amount'] = $_POST['accessoryamount'][$i];
								$accessorymap['deal'] = $_POST['accessorydeal'][$i];
								$accessorymap['preparation'] = $_POST['accessorypreparation'][$i];
								$accessorymap['foodsectionid'] = $_POST['accessoryfoodsectionid'][$i];
								$accessorymap['foodcategoryid'] = $_POST['accessoryfoodcategoryid'][$i];
								$accessorymap['foodarticleid'] = $_POST['accessoryfoodarticleid'][$i];
								$accessorymap['foodname'] = $_POST['other'][$i];
								$accessorymap['recipecategoryid'] = $_POST['recipecategoryid'];
								$accessorymap['recipetype'] = $_POST['recipetype'];
								$accessorymap['recipetypeid'] = $_POST['recipetypeid'];
								$accessorymap['recipearticleid'] = $recipeid;
								//	dump($accessorymap);
								$recipeaccessory->add($accessorymap);
							}
						}

						for($i=0; $i<count($_POST['condiment']); $i++){
							if(empty($_POST['condiment'][$i])){
								continue;
							}else{
								$condimentmap['amount'] = $_POST['condimentamount'][$i];
								$condimentmap['deal'] = $_POST['condimentdeal'][$i];
								$condimentmap['preparation'] = $_POST['condimentpreparation'][$i];
								$condimentmap['foodsectionid'] = $_POST['condimentfoodsectionid'][$i];
								$condimentmap['foodcategoryid'] = $_POST['condimentfoodcategoryid'][$i];
								$condimentmap['foodarticleid'] = $_POST['condimentfoodarticleid'][$i];
								$condimentmap['foodname'] = $_POST['condiment'][$i];
								$condimentmap['recipecategoryid'] = $_POST['recipecategoryid'];
								$condimentmap['recipetype'] = $_POST['recipetype'];
								$condimentmap['recipetypeid'] = $_POST['recipetypeid'];
								$condimentmap['recipearticleid'] = $recipeid;
								//	dump($condimentmap);
								$recipecondiment->add($condimentmap);
							}
						}
						$this->success("发布菜谱成功",__APP__.'/Special/content/cid/'.$_POST['recipecategoryid'].'/id/'.$recipeid);
					}else{
						$this->error('发布菜谱失败');
					}
				}else{
					$this->error($recipe->getError());
				}
			}else{
				$this->error('异常操作');
			}
		}else{
			$this->error('异常操作');
		}

	}

	function update(){
		if(isset($_POST['id']) && !empty($_POST['id'])){
			$tmpdir = './Public/Content/tmp/';
			$srcdir = './Public/Content/SpecialArticle/';
			$recipe = D('SpecialArticle');

			$recipemain = M('RecipeMain');
			$recipeaccessory = M('RecipeAccessory');
			$recipecondiment = M('RecipeCondiment');

			$recipenutritions = M('RecipeNutritions');

			$recipeinfos = $recipe->relation(true)->find($_POST['id']);
			if($recipeinfo = $recipe->create()){

				if($recipe->save()){

					$deleterecipenutritions['sectionid'] = $recipeinfos['recipetypeid'];
					$deleterecipenutritions['categoryid'] = $recipeinfos['recipecategoryid'];
					$deleterecipenutritions['articleid'] = $_POST['id'];

					if($recipenutritions->where($deleterecipenutritions)->delete()){
						$orgdatas = $_POST;
						$returndatas = $this->calculate($orgdatas);
						$returndatas['sectionid'] = 2;
						$returndatas['categoryid'] = $_POST['recipecategoryid'];
						$returndatas['articleid'] = $_POST['id'];
						$recipenutritions->add($returndatas);
					}

					if($recipeinfo['image']!=$recipeinfos['image']){

						unlink($srcdir.'Big_'.$recipeinfos['image']);
						unlink($srcdir.'Small_'.$recipeinfos['image']);

						copy($tmpdir.'Big_'.$recipeinfo['image'], $srcdir.'Big_'.$recipeinfo['image']);
						copy($tmpdir.'Small_'.$recipeinfo['image'], $srcdir.'Small_'.$recipeinfo['image']);

						if($dirsrc = opendir($tmpdir)){
							while($files = readdir($dirsrc)){
								if($files!='.' && $files!='..'){
									$pathfile = $tmpdir.$files;
									if(is_file($pathfile)){
										unlink($pathfile);
									}
								}
							}
						}

					}

					$deletes['recipecategoryid'] = $recipeinfos['recipecategoryid'];
					$deletes['recipearticleid'] = $recipeinfos['id'];
					$deletes['recipetypeid'] = $recipeinfos['recipetypeid'];


					if($recipemain->where($deletes)->delete() || empty($recipeinfos['recipemain'])){
						for($i=0; $i<count($_POST['main']); $i++){
							if(empty($_POST['main'][$i])){
								continue;
							}else{
								$mainmap['amount'] = $_POST['mainamount'][$i];
								$mainmap['deal'] = $_POST['maindeal'][$i];
								$mainmap['preparation'] = $_POST['mainpreparation'][$i];
								$mainmap['foodsectionid'] = $_POST['mainfoodsectionid'][$i];
								$mainmap['foodcategoryid'] = $_POST['mainfoodcategoryid'][$i];
								$mainmap['foodarticleid'] = $_POST['mainfoodarticleid'][$i];
								$mainmap['foodname'] = $_POST['main'][$i];
								$mainmap['recipecategoryid'] = $_POST['recipecategoryid'];
								$mainmap['recipetype'] = $_POST['recipetype'];
								$mainmap['recipetypeid'] = $_POST['recipetypeid'];
								$mainmap['recipearticleid'] = $_POST['id'];

								$recipemain->add($mainmap);
							}
						}

					}

					if($recipeaccessory->where($deletes)->delete() || empty($recipeinfos['recipeaccessory'])){
						for($i=0; $i<count($_POST['other']); $i++){
							if(empty($_POST['other'][$i])){
								continue;
							}else{
								$accessorymap['amount'] = $_POST['accessoryamount'][$i];
								$accessorymap['deal'] = $_POST['accessorydeal'][$i];
								$accessorymap['preparation'] = $_POST['accessorypreparation'][$i];
								$accessorymap['foodsectionid'] = $_POST['accessoryfoodsectionid'][$i];
								$accessorymap['foodcategoryid'] = $_POST['accessoryfoodcategoryid'][$i];
								$accessorymap['foodarticleid'] = $_POST['accessoryfoodarticleid'][$i];
								$accessorymap['foodname'] = $_POST['other'][$i];
								$accessorymap['recipecategoryid'] = $_POST['recipecategoryid'];
								$accessorymap['recipetype'] = $_POST['recipetype'];
								$accessorymap['recipetypeid'] = $_POST['recipetypeid'];
								$accessorymap['recipearticleid'] = $_POST['id'];

								$recipeaccessory->add($accessorymap);
							}
						}
					}

					if($recipecondiment->where($deletes)->delete() || empty($recipeinfos['recipecondiment'])){
						for($i=0; $i<count($_POST['condiment']); $i++){
							if(empty($_POST['condiment'][$i])){
								continue;
							}else{
								$condimentmap['amount'] = $_POST['condimentamount'][$i];
								$condimentmap['deal'] = $_POST['condimentdeal'][$i];
								$condimentmap['preparation'] = $_POST['condimentpreparation'][$i];
								$condimentmap['foodsectionid'] = $_POST['condimentfoodsectionid'][$i];
								$condimentmap['foodcategoryid'] = $_POST['condimentfoodcategoryid'][$i];
								$condimentmap['foodarticleid'] = $_POST['condimentfoodarticleid'][$i];
								$condimentmap['foodname'] = $_POST['condiment'][$i];
								$condimentmap['recipecategoryid'] = $_POST['recipecategoryid'];
								$condimentmap['recipetype'] = $_POST['recipetype'];
								$condimentmap['recipetypeid'] = $_POST['recipetypeid'];
								$condimentmap['recipearticleid'] = $_POST['id'];
							//	dump($condimentmap);
								$recipecondiment->add($condimentmap);
							}
						}
					}
				//	$this->assign('waitSecond',500);
					$this->success('修改菜谱成功',__APP__.'/Special/content/cid/'.$recipeinfos['recipecategoryid'].'/id/'.$_POST['id']);
				}else{
					$this->error('修改菜谱失败');
				}
			}

		}else{
			$this->error('异常操作');
		}
	}

	function deletecomments(){

		if(isset($_GET['sid']) && !empty($_GET['sid'])){
			$comments = M('SpecialComments');
			if($comments->delete($_GET['sid'])){
				$this->success('删除成功');
			}else{
				$this->error('删除失败');
			}
		}else{
			$this->error('异常操作');
		}
	}

	function comments(){

		if(isset($_POST['categoryid']) && isset($_POST['articleid'])){
			if(!empty($_POST['categoryid']) && !empty($_POST['articleid'])){
				$comments = M('SpecialComments');
				$recipe = M('SpecialArticle');
				$recipeinfo = $recipe->where('recipecategoryid='.$_POST['categoryid'].' AND '.'id='.$_POST['articleid'])->find();
				$user = M('User');
				if(empty($_POST['code'])){
					if($commentsinfo = $comments->create()){
						$time = time();
						$commentsinfo['created'] = $time;
						if(empty($commentsinfo['username'])){
							$commentsinfo['username'] = 'Anonymous';
						}else{
							$userinfo = $user->getByUsername($commentsinfo['username']);
						}

						if($comments->add($commentsinfo)){
							if($recipeinfo['founder']!=$userinfo['id']){
								$user->where('id='.$userinfo['id'])->setInc('exp',5);
							}
							$this->success("添加添加评论成功");
						}else{
							$this->error('添加评论失败');
						}
					}else{
						$this->error($comments->getError());
					}
				}else if(md5($_POST['code'])==$_SESSION['verify']){
					if($commentsinfo = $comments->create()){
						$time = time();
						$commentsinfo['created'] = $time;
						if(empty($commentsinfo['username'])){
							$commentsinfo['username'] = 'Anonymous';
						}
						if($comments->add($commentsinfo)){
							$this->success("添加添加评论成功");
						}else{
							$this->error('添加评论失败');
						}
					}else{
						$this->error($comments->getError());
					}
				}else{
					$this->error('验证码错误');
				}
			}else{
				$this->error('异常操作');
			}
		}else{
			$this->error('异常操作');
		}

	}

	function calculate($data){
		$fooddeal = D('FoodDeal');
		$foodnutritions = M('FoodNutritions');

		$mainsum = array();
		$accessorysum = array();
		$condimentsum = array();
		$totals = array();

		for($mi=0; $mi<count($data['mainamount']); $mi++){

			if(empty($data['mainamount'][$mi])){
				continue;
			}else{
				$map['sectionid'] = $data['mainfoodsectionid'][$mi];
				$map['categoryid'] = $data['mainfoodcategoryid'][$mi];
				$map['articleid'] = $data['mainfoodarticleid'][$mi];
				$map['unit'] = $data['maindeal'][$mi];
				$mainfooddealinfo[$mi] = $fooddeal->relation(true)->where($map)->find();
				$foodnutritionsinfo[$mi] = $foodnutritions->where('articleid='.$map['articleid'])->find();
			}

			$mainfooddealinfo[$mi]['sum']=$data['mainamount'][$mi]*$mainfooddealinfo[$mi]['deal'];
			$mainfooddealinfo[$mi]['infos'] = $foodnutritionsinfo[$mi];
			unset($mainfooddealinfo[$mi]['infos']['id']);
			unset($mainfooddealinfo[$mi]['infos']['sectionid']);
			unset($mainfooddealinfo[$mi]['infos']['categoryid']);
			unset($mainfooddealinfo[$mi]['infos']['articleid']);

			foreach($mainfooddealinfo[$mi]['infos'] as $key=>$value){
				$mainfooddealinfo[$mi]['infos'][$key] = round($mainfooddealinfo[$mi]['sum']*($value/100),2);
				$mainsum[$key]+=$mainfooddealinfo[$mi]['infos'][$key];
			}

		}

		for($ma=0; $ma<count($data['accessoryamount']); $ma++){
			if(empty($data['accessoryamount'][$ma])){
				continue;
			}else{
				$mapa['sectionid'] = $data['accessoryfoodsectionid'][$ma];
				$mapa['categoryid'] = $data['accessoryfoodcategoryid'][$ma];
				$mapa['articleid'] = $data['accessoryfoodarticleid'][$ma];
				$mapa['unit'] = $data['accessorydeal'][$ma];
				$accessoryfooddealinfo[$ma] = $fooddeal->relation(true)->where($mapa)->find();
				$foodnutritionsinfo[$ma] = $foodnutritions->where('articleid='.$mapa['articleid'])->find();
			}

			$accessoryfooddealinfo[$ma]['sum']=$data['accessoryamount'][$ma]*$accessoryfooddealinfo[$ma]['deal'];
			$accessoryfooddealinfo[$ma]['infos'] = $foodnutritionsinfo[$ma];
			unset($accessoryfooddealinfo[$ma]['infos']['id']);
			unset($accessoryfooddealinfo[$ma]['infos']['sectionid']);
			unset($accessoryfooddealinfo[$ma]['infos']['categoryid']);
			unset($accessoryfooddealinfo[$ma]['infos']['articleid']);

			foreach($accessoryfooddealinfo[$ma]['infos'] as $key=>$value){
				$accessoryfooddealinfo[$ma]['infos'][$key] = round($accessoryfooddealinfo[$ma]['sum']*($value/100),2);
				$accessorysum[$key]+=$accessoryfooddealinfo[$ma]['infos'][$key];
			}

		}

		for($mc=0; $mc<count($data['condimentamount']); $mc++){
			if(empty($data['condimentamount'][$mc])){
				continue;
			}else{
				$mapc['sectionid'] = $data['condimentfoodsectionid'][$mc];
				$mapc['categoryid'] = $data['condimentfoodcategoryid'][$mc];
				$mapc['articleid'] = $data['condimentfoodarticleid'][$mc];
				$mapc['unit'] = $data['condimentdeal'][$mc];
				$condimentfooddealinfo[$mc] = $fooddeal->relation(true)->where($mapc)->find();
				$foodnutritionsinfo[$mc] = $foodnutritions->where('articleid='.$mapc['articleid'])->find();
			}

			$condimentfooddealinfo[$mc]['sum']=$data['condimentamount'][$mc]*$condimentfooddealinfo[$mc]['deal'];
			$condimentfooddealinfo[$mc]['infos'] = $foodnutritionsinfo[$mc];
			unset($condimentfooddealinfo[$mc]['infos']['id']);
			unset($condimentfooddealinfo[$mc]['infos']['sectionid']);
			unset($condimentfooddealinfo[$mc]['infos']['categoryid']);
			unset($condimentfooddealinfo[$mc]['infos']['articleid']);

			foreach($condimentfooddealinfo[$mc]['infos'] as $key=>$value){
				$condimentfooddealinfo[$mc]['infos'][$key] = round($condimentfooddealinfo[$mc]['sum']*($value/100),2);
				$condimentsum[$key]+=$condimentfooddealinfo[$mc]['infos'][$key];
			}

		}

//		for($ms=0; $ms<count($mainfooddealinfo); $ms++){
//			foreach($mainfooddealinfo[$ms]['infos'] as $key=>$value){
//				$mainfooddealinfo[$ms]['infos'][$key] = round($mainfooddealinfo[$ms]['sum']*($value/100),2);
//				$mainsum[$key]+=$mainfooddealinfo[$ms]['infos'][$key];
//			}
//		}

		$condiment = array_values($condimentsum);
		$accessory = array_values($accessorysum);
		$main = array_values($mainsum);

		for($t=0; $t<count(array_values($mainsum)); $t++){
			$totals[] = $main[$t]+$accessory[$t]+$condiment[$t];
		}

		$innernutritions['calories'] = $totals[0];
		$innernutritions['total-fat'] = $totals[1];
		$innernutritions['saturated-fat'] = $totals[2];
		$innernutritions['cholesterol'] = $totals[3];
		$innernutritions['sodium'] = $totals[4];
		$innernutritions['potassium'] = $totals[5];
		$innernutritions['total-carbohydrate'] = $totals[6];
		$innernutritions['dietary-fiber'] = $totals[7];
		$innernutritions['sugars'] = $totals[8];
		$innernutritions['protein'] = $totals[9];
		$innernutritions['vitamin-a'] = $totals[10];
		$innernutritions['vitamin-b-12'] = $totals[11];
		$innernutritions['vitamin-b-6'] = $totals[12];
		$innernutritions['vitamin-c'] = $totals[13];
		$innernutritions['vitamin-d'] = $totals[14];
		$innernutritions['vitamin-e'] = $totals[15];
		$innernutritions['calcium'] = $totals[16];
		$innernutritions['copper'] = $totals[17];
		$innernutritions['folate'] = $totals[18];
		$innernutritions['iron'] = $totals[19];
		$innernutritions['magnesium'] = $totals[20];
		$innernutritions['manganese'] = $totals[21];
		$innernutritions['niacin'] = $totals[22];
		$innernutritions['pantothenic-acid'] = $totals[23];
		$innernutritions['phosphorus'] = $totals[24];
		$innernutritions['riboflavin'] = $totals[25];
		$innernutritions['selenium'] = $totals[26];
		$innernutritions['thiamin'] = $totals[27];
		$innernutritions['zinc'] = $totals[28];

		return $innernutritions;
//		echo json_encode(array_values($totals));

	}

}